#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <json/json.h>
#include <order.h>

using namespace std;
static int nextOrderNumber=0;

Order::Order(std::string buyer, std::string product, std::string status)
    : buyerInfo(buyer), productInfo(product), orderStatus(status) {
    orderNumber=CreateOrderNumber();
}

std::string Order::getOrderNumber() const {
    return orderNumber;
}

std::string Order::getBuyerInfo() const {
    return buyerInfo;
}

std::string Order::getProductInfo() const {
    return productInfo;
}

std::string Order::getOrderStatus() const {
    return orderStatus;
}

void Order::setOrderStatus(std::string status) {
    orderStatus = status;
}

void Order::setBuyerInfo(std::string buyer) {
    buyerInfo = buyer;
}

void Order::setProductInfo(std::string product) {
    productInfo = product;
}

void Order::displayOrder() const {
    std::cout << "Order Number: " << orderNumber << std::endl;
    std::cout << "Buyer Info: " << buyerInfo << std::endl;
    std::cout << "Product Info: " << productInfo << std::endl;
    std::cout << "Order Status: " << orderStatus << std::endl;
    std::cout << "——————————————————————————————— " << std::endl;
}

Json::Value Order::toJson() const {
    Json::Value jsonData;
    jsonData["orderNumber"] = orderNumber;
    jsonData["buyerInfo"] = buyerInfo;
    jsonData["productInfo"] = productInfo;
    jsonData["orderStatus"] = orderStatus;
    return jsonData;
}

void Order::fromJson(const Json::Value& jsonData) {
    orderNumber = jsonData["orderNumber"].asString();
    buyerInfo = jsonData["buyerInfo"].asString();
    productInfo = jsonData["productInfo"].asString();
    orderStatus = jsonData["orderStatus"].asString();
}

string Order::CreateOrderNumber()
{
    std::string formattedOrderNumber = "";

    // 使用 std::setw() 设置输出宽度为 5，并用 '0' 填充空位
    formattedOrderNumber = std::to_string(nextOrderNumber);
    formattedOrderNumber = std::string(5 - formattedOrderNumber.length(), '0') + formattedOrderNumber;

    nextOrderNumber++;  // 增加下一个订单编号的值

    return formattedOrderNumber;
}



OrderManager::OrderManager(std::string file) : filename(file) {
    loadOrders();
}

void OrderManager::loadOrders() {
    std::ifstream fileStream(filename);
    if (fileStream.is_open()) {
        Json::Value jsonData;
        Json::CharReaderBuilder reader;
        std::string errors;
        if (Json::parseFromStream(reader, fileStream, &jsonData, &errors)) {
            for (const auto& item : jsonData) {
                Order order("", "", "");
                order.fromJson(item);
                orders.push_back(order);
            }
        } else {
            std::cout << "Failed to parse JSON file: " << errors << std::endl;
        }
        fileStream.close();
    } else {
        std::cout << "Unable to open file for loading orders." << std::endl;
    }
}

void OrderManager::addOrder(const Order& order) {
    orders.push_back(order);
}

void OrderManager::cancelOrder(const std::string& orderNumber) {
    for (auto& order : orders) {
        if (order.getOrderNumber() == orderNumber) {
            order.setOrderStatus("Cancelled");
            break;
        }
    }
}

void OrderManager::modifyOrderStatus(const std::string& orderNumber, const std::string& status) {
    for (auto& order : orders) {
        if (order.getOrderNumber() == orderNumber) {
            order.setOrderStatus(status);
            break;
        }
    }
}

void OrderManager::modifyOrderBuyer(const std::string& orderNumber, const std::string& buyer) {
    for (auto& order : orders) {
        if (order.getOrderNumber() == orderNumber) {
            order.setBuyerInfo(buyer);
            break;
        }
    }
}

void OrderManager::modifyOrderProduct(const std::string& orderNumber, const std::string& product) {
    for (auto& order : orders) {
        if (order.getOrderNumber() == orderNumber) {
            order.setProductInfo(product);
            break;
        }
    }
}

void OrderManager::displayOrders() const {
    for (const auto& order : orders) {
        order.displayOrder();
    }
}

void OrderManager::saveOrders() const {
    Json::Value jsonData(Json::arrayValue);
    for (const auto& order : orders) {
        jsonData.append(order.toJson());
    }

    std::ofstream file(filename);
    if (file.is_open()) {
        Json::StreamWriterBuilder writer;
        writer["indentation"] = "\t";
        std::unique_ptr<Json::StreamWriter> jsonWriter(writer.newStreamWriter());
        jsonWriter->write(jsonData, &file);
        file.close();
    } else {
        std::cout << "Unable to open file for saving orders." << std::endl;
    }
}

bool OrderManager::findOrdersByNum(const string& orderNumber) const
{
    if(orderNumber.empty())
    {
        cout<<"Input Error!"<<endl;
    }

    string number=orderNumber;
    bool found=false;

    for (auto& order : orders) {
        if (order.getOrderNumber() == number) {
            order.displayOrder();
            found=true;
        }
    }

    return found;
}

bool OrderManager::findOrdersByBuyer(const string& orderBuyer) const
{
    string buyer=orderBuyer;
    bool found = false;  // 标记是否找到匹配的订单

    if(orderBuyer.empty())
    {
        cout<<"Input Error!"<<endl;
    }

    for (auto& order : orders) {
        if (order.getBuyerInfo() == buyer) {
            order.displayOrder();
            found=true;
        }
    }

    return found;
}

